home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / zoo tutorial / module 5- custom controllers / source / animalpane.java next >
Encoding:
Java Source  |  2000-09-28  |  7.5 KB  |  223 lines

  1. import java.awt.Dimension;
  2. import java.io.IOException;
  3. import java.io.FileNotFoundException;
  4.  
  5. import quicktime.app.QTFactory;
  6. import quicktime.app.anim.Compositor;
  7. import quicktime.app.image.GraphicsImporterDrawer; 
  8. import quicktime.app.image.ImagePresenter; 
  9. import quicktime.app.image.ImageUtil;
  10. import quicktime.app.players.MoviePresenter;
  11. import quicktime.app.players.QTPlayer;
  12.  
  13. import quicktime.io.QTFile;
  14. import quicktime.io.OpenMovieFile;
  15.  
  16. import quicktime.qd.QDRect;
  17. import quicktime.qd.QDGraphics;
  18. import quicktime.qd.QDColor; 
  19. import quicktime.qd.QDConstants; 
  20.  
  21. import quicktime.qd.QDDrawer;
  22.  
  23. import quicktime.std.image.Matrix;
  24. import quicktime.std.StdQTConstants;
  25. import quicktime.std.movies.Movie; 
  26. import quicktime.std.image.GraphicsMode;
  27.  
  28. import quicktime.QTSession;
  29. import quicktime.QTException; 
  30.  
  31. /**
  32.  * QTZoo Module 4 - Playing a sound file
  33.  * This application requires QuickTime for Java *
  34.  * @author Levi Brown
  35.  * @author Michael Hopkins
  36.  * @author Apple Computer, Inc.
  37.  * @version 3.1 11/16/1999
  38.  *
  39.  * Copyright:     © Copyright 1999 Apple Computer, Inc. All rights reserved.
  40.  *    
  41.  * Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  42.  *                ("Apple") in consideration of your agreement to the following terms, and your
  43.  *                use, installation, modification or redistribution of this Apple software
  44.  *                constitutes acceptance of these terms.  If you do not agree with these terms,
  45.  *                please do not use, install, modify or redistribute this Apple software.
  46.  *
  47.  *                In consideration of your agreement to abide by the following terms, and subject
  48.  *                to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  49.  *                copyrights in this original Apple software (the "Apple Software"), to use,
  50.  *                reproduce, modify and redistribute the Apple Software, with or without
  51.  *                modifications, in source and/or binary forms; provided that if you redistribute
  52.  *                the Apple Software in its entirety and without modifications, you must retain
  53.  *                this notice and the following text and disclaimers in all such redistributions of
  54.  *                the Apple Software.  Neither the name, trademarks, service marks or logos of
  55.  *                Apple Computer, Inc. may be used to endorse or promote products derived from the
  56.  *                Apple Software without specific prior written permission from Apple.  Except as
  57.  *                expressly stated in this notice, no other rights or licenses, express or implied,
  58.  *                are granted by Apple herein, including but not limited to any patent rights that
  59.  *                may be infringed by your derivative works or by other works in which the Apple
  60.  *                Software may be incorporated.
  61.  *
  62.  *                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  63.  *                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  64.  *                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  65.  *                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  66.  *                COMBINATION WITH YOUR PRODUCTS.
  67.  *
  68.  *                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  69.  *                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  70.  *                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  71.  *                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  72.  *                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  73.  *                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  74.  *                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  75.  * 
  76.  * Revision History
  77.  * ---------------------------------------------------------------------------------
  78.  * 12/02/99 MSH  cleaned up code, added comments
  79.  * 
  80.  */
  81.  
  82. public class AnimalPane
  83. {
  84.     /**
  85.      *  Public default constructor
  86.      *  Creates the map button, and sets up the listeners
  87.      */
  88.     public AnimalPane()
  89.     {
  90.         final QDRect size = new QDRect( Zoo4.WIDTH, Zoo4.HEIGHT );
  91.         try
  92.         {
  93.             loadSound( "data/zebra/Zebra.au" );
  94.  
  95.             QDGraphics gw = new QDGraphics( size );            // create a new graphics object
  96.             compositor = new Compositor( gw, QDColor.white, 30, 1 );
  97.             
  98.             QTFile imageFile = new QTFile( QTFactory.findAbsolutePath( "data/zebra/ZebraBackground.jpg" ));
  99.             GraphicsImporterDrawer drawer = new GraphicsImporterDrawer( imageFile );
  100.             ImagePresenter presenter = ImagePresenter.fromGraphicsImporterDrawer( drawer );
  101.             presenter.setLocation( 110, 110 );                // set location of movie within compositor    
  102.             compositor.addMember( presenter, 3 );            // add image presenter to compositor in 2nd layer
  103.             
  104.             addText( "data/zebra/Zebra.txt", 2, 15, 160, 415, 220 );  
  105.             
  106.             Movie m = makeMovie( new QTFile( QTFactory.findAbsolutePath( "data/zebra/Zebra.mov" )));
  107.             md = new MoviePresenter( m );                    // create presenter from movie file    
  108.             compositor.addMember( md, 1 );                    // add presenter to compositor
  109.             compositor.getTimer().setRate(1);                // start compositor
  110.             md.setRate(1);                                    // start movie
  111.  
  112.             playSound();
  113.  
  114.         }
  115.         catch( IOException e )                                // catch any errors
  116.         {
  117.             e.printStackTrace();
  118.         }
  119.         catch( QTException e )
  120.         {
  121.             e.printStackTrace();
  122.         }
  123.     }
  124.  
  125.  
  126.     /**
  127.      *  Plays the sound file associated with the current AnimalPane
  128.      */
  129.     public void playSound()
  130.     {
  131.         if( player == null )
  132.             return;
  133.  
  134.         try
  135.         {
  136.             player.setTime(0);            //Start the sound at the beginning
  137.             player.startTasking();         //Make sure the player gets time to play the sound
  138.             player.setRate(1);             //Start playing
  139.         }
  140.         catch( QTException e )
  141.         {
  142.             e.printStackTrace();
  143.         }
  144.     }
  145.     
  146.     /**
  147.      *    Return the compositor associated with this pane
  148.      */
  149.     public Compositor getCompositor( ) { return compositor; }
  150.  
  151.     
  152.     /**
  153.      * Reads a text file from the disk and displays it in the compositor
  154.      * @param textPath the relative path where the text is located
  155.      * @param layer, the layer of the compositor to add the text to.
  156.      * @param x the x coordinate location.
  157.      * @param y the y coordinate location.
  158.      * @param width the width of the area to display the text.
  159.      * @param height the height of the area to display the text.
  160.      */
  161.     protected void addText( String textPath, int layer, int x, int y, int width, int height )
  162.     {
  163.         try
  164.         {
  165.             TextPresenter text = new TextPresenter( textPath, new Dimension( width, height ));
  166.             
  167.             ImagePresenter presenter = text.getPresenter();
  168.             Matrix theMatrix = new Matrix();
  169.             theMatrix.translate( (float) x, (float) y );
  170.             presenter.setMatrix( theMatrix );
  171.             compositor.addMember( presenter, layer );
  172.         }
  173.         catch ( QTException e )
  174.         {
  175.             e.printStackTrace();
  176.         }
  177.         catch ( FileNotFoundException e )
  178.         {
  179.             e.printStackTrace();
  180.         }    
  181.     }    // MODULE 3 Addition
  182.  
  183.  
  184.     /**
  185.      * Loads the specified sound file from disk and prepares it for playing
  186.      */
  187.     protected void loadSound( String soundPath )
  188.     {
  189.         try
  190.         {
  191.             String soundLocation = QTFactory.findAbsolutePath( soundPath ).getPath();
  192.             //this call works with a file://, http://, rtsp://located movie
  193.             player = (QTPlayer)QTFactory.makeDrawable ("file://" + soundLocation);
  194.         }
  195.         catch( IOException e )
  196.         {
  197.             e.printStackTrace();
  198.         }
  199.         catch( QTException e )
  200.         {
  201.             e.printStackTrace();
  202.         }
  203.     }
  204.         
  205.     /**
  206.      * Opens the movie file and sets it up to be played.
  207.      * @param f a QTFile representing the movie to initialize.
  208.      * @return the movie, ready to play
  209.      */
  210.     protected Movie makeMovie( QTFile f ) throws IOException, QTException
  211.     {
  212.         OpenMovieFile movieFile = OpenMovieFile.asRead(f);
  213.         Movie m = Movie.fromFile( movieFile );
  214.         m.getTimeBase().setFlags( StdQTConstants.loopTimeBase );    
  215.         return m;    
  216.     }
  217.     
  218.     protected QTPlayer player;        
  219.  
  220.     protected Compositor compositor;
  221.     protected MoviePresenter md;
  222. }
  223.